home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / bin / list-devices < prev    next >
Text File  |  2009-09-17  |  3KB  |  115 lines

  1. #! /bin/sh -e
  2. TYPE="$1"
  3.  
  4. case $TYPE in
  5.     maybe-floppy)
  6.     logger -t list-devices "deprecated parameter maybe-floppy"
  7.     TYPE=floppy
  8.     ;;
  9.     cd|disk|partition|floppy|maybe-usb-floppy|usb-partition|mmc-partition) ;;
  10.     *)
  11.     echo "Usage: $0 cd|disk|partition|floppy|maybe-usb-floppy|usb-partition|mmc-partition" >&2
  12.     exit 2
  13.     ;;
  14. esac
  15.  
  16. if [ ! -d /sys/block ]; then
  17.     exit 0
  18. fi
  19. if type udevadm >/dev/null 2>&1; then
  20.     device_info () {
  21.         udevadm info -q "$1" -p "$2" 2>/dev/null
  22.     }
  23. elif type udevinfo >/dev/null 2>&1; then
  24.     device_info () {
  25.         udevinfo -q "$1" -p "$2" 2>/dev/null
  26.     }
  27. else
  28.     exit 0
  29. fi
  30.  
  31. syspaths=
  32. scan_partition=false
  33. case $TYPE in
  34.     partition)
  35.     for x in /sys/block/*/*[0-9]; do
  36.         [ -d "$x" ] || continue
  37.         syspaths="${syspaths:+$syspaths }$x"
  38.     done
  39.     TYPE=disk
  40.     # Also allow misdetected USB devices
  41.     scan_partition=:
  42.     ;;
  43.     usb-partition|mmc-partition)
  44.     for x in /sys/block/*/*; do
  45.         [ -d "$x" ] || continue
  46.         syspaths="${syspaths:+$syspaths }$x"
  47.     done
  48.     ;;
  49.     *)
  50.     for x in /sys/block/*; do
  51.         [ -d "$x" ] || continue
  52.         syspaths="${syspaths:+$syspaths }$x"
  53.     done
  54.     ;;
  55. esac
  56. for x in $syspaths; do
  57.     devpath="${x#/sys}"
  58.     match=false
  59.     case $TYPE in
  60.         floppy)
  61.         # TODO ugly special case for non-IDE floppies
  62.         case $devpath in
  63.             /block/fd[0-9]*)
  64.             match=:
  65.             ;;
  66.         esac
  67.         ;;
  68.     esac
  69.     if ! $match && [ "$TYPE" = cd ]; then
  70.         if device_info env "$devpath" | grep -q '^ID_CDROM='; then
  71.             match=:
  72.         fi
  73.     fi
  74.     if ! $match; then
  75.         if device_info env "$devpath" | grep -q "^ID_TYPE=$TYPE"; then
  76.             match=:
  77.         fi
  78.     fi
  79.     if ! $match && [ "$TYPE" = disk ]; then
  80.         case $devpath in
  81.             /block/cciss\!*|/block/ida\!*|/block/rd\!*|/block/mmcblk*)
  82.             match=:
  83.             ;;
  84.         esac
  85.     fi
  86.     # Some USB sticks and CD drives are misdetected as floppy
  87.     # This allows to scan for those
  88.     if ! $match && ( $scan_partition || [ "$TYPE" = maybe-usb-floppy ] ); then
  89.         if device_info env "$devpath" | grep -q '^ID_BUS=usb' && \
  90.            device_info env "$devpath" | grep -q '^ID_TYPE=floppy'; then
  91.             match=:
  92.         fi
  93.     fi
  94.     # Disk partitions, but only on USB drives
  95.     if ! $match && [ "$TYPE" = usb-partition ]; then
  96.         if device_info env "$devpath" | grep -q '^ID_BUS=usb' && \
  97.            device_info env "$devpath" | grep -q '^ID_TYPE=disk'; then
  98.             match=:
  99.         fi
  100.     fi
  101.     # Disk partitions, but only on (non-USB) MMC devices
  102.     if ! $match && [ "$TYPE" = mmc-partition ]; then
  103.         if device_info env "$devpath" | egrep -q '^ID_PATH=(platform-mmc|platform-orion-ehci|platform-mxsdhci)'; then
  104.             match=:
  105.         fi
  106.     fi
  107.     if $match; then
  108.         if ! name="$(device_info name "$devpath")"; then
  109.             name="$(printf %s "${devpath##*/}" | \
  110.                 sed 's,!,/,g')"
  111.         fi
  112.         echo "/dev/$name"
  113.     fi
  114. done
  115.